Macromedia Flex Article

Learning Flex Basics (Part 4): Working with Data Models


Table of Contents

  1. Introduction
  2. Create the Application Object and Application Layout
  3. Add the Form Controls and the DataGrid Controls
  4. Add the New Employee Model, Employee, and Location Models
  5. Instantiate the DataController Class, and Create the Employee and DataController Classes
  6. Add the DataController Methods
  7. Code Listing for data.mxml
  8. Code Listings for DataController.as and Employee.as
  9. Code Listings for locations.xml and employee.xml
  10. Where to Go From Here

Add the Form Controls

The Form container and its child containers are covered in Learning Flex Basics (Part 3): Working with Containers. Here, you add controls for entry of new employee data: a couple of TextInput fields and a ComboBox whose dataProvider will be bound to locModel.location; you generate this array from an imported XML file in a later part of the tutorial.

After the form, add a button that appears to submit the form data, though you will see later that this not exactly what it does.

  1. Inside the Form container block add a FormHeading with the label Add New:

    <mx:FormHeading label="Add New"/>
    
  2. After the FormHeading, add a FormItem with the label First Name:

    <mx:FormItem label="First Name">
    </mx:FormItem>
    	
  3. Inside the FormItem container block, add a TextInput with the id of fNameInput:

    <mx:TextInput id="fNameInput"/>
  4. Add another FormItem with the label Last Name:

    <mx:FormItem label="Last Name">
    </mx:FormItem>
    
  5. Inside the FormItem container block, add a TextInput with the id of lNameInput:

    <mx:TextInput id="lNameInput"/>
  6. Add another FormItem with the label Location:

    <mx:FormItem label="Location">
    </mx:FormItem>
    
  7. Inside the FormItem container block, add a ComboBox with the id of locCombo and the dataProvider bound to locModel.location:

    <mx:ComboBox id="locCombo" dataProvider="{locModel.location}"/>
  8. Inside the ControlBar container that follows the Form block, add a Button with id and click properties as shown:

    <mx:Button label="Add Employee" click="dataController.addNewEmp(newEmpModel,empGrid.dataProvider)"/>
    

    Note: You will add the dataController and its method later - the arguments for the method will make sense then.

Add the DataGrid Controls

You now add instances of a class not used in the previous tutorials: the DataGrid control. The DataGrid control is a powerful data visualization tool; its columns are sortable and cells are editable. The DataGrid control takes an array of objects as its dataProvider. The columns are specified inside a nested <columns> block as an array of DataGridColumn containers. The columnName property value will be one of the properties of the objects in the dataProvider. The general syntax for the DataGrid control is as follows:

<mx:DataGrid>
	<mx:dataProvider>
		{some array of objects}
	</mx:dataProvider>
	<mx:columns>
		<mx:Array>
			<mx:DataGridColumn columnName="propertyName1" headerText="some text" />
			<mx:DataGridColumn columnName="propertyName2" headerText="some text" />
		</mx:Array>
	</mx:columns>
</mx:DataGrid>

To enable drag and drop of DataGrid items, set dragEnabled="true". To enable users to drag data objects and drop them into a grid, there are several drag-related events you can set handlers for, but the essential ones are:

  • dragEnter: broadcast when an item is dragged over the grid
  • dragDrop: broadcast when an item is dropped onto the grid

In the following steps, you will enable users to drag items from an employee grid and drop them onto either an engineering or support grid. You will write the handlers for the events as methods of the controller later.

  1. Inside the first Panel container, above the Form container block, add a DataGrid container with the id="empGrid" and dragEnabled="true":

    <mx:DataGrid id="empGrid" dragEnabled="true">
    </mx:DataGrid>
    
  2. Inside the DataGrid container, add a dataProvider block with a binding to empModel.employee:

    <mx:dataProvider>
    	{empModel.employee}
    </mx:dataProvider>
    

    Note: You will generate the data for the dataProvider later.

  3. After the dataProvider block, but still inside the DataGrid block, add a columns block with a nested Array block:

    <mx:columns>
    	<mx:Array>
    	</mx:Array>
    </mx:columns>
    
  4. Inside the Array block, add a DataGridColumn with name fName and headerText First Name:

    <mx:DataGridColumn columnName="fName" headerText="First Name" />
    
  5. Add two more DataGridColumn containers with the following properties:

    • columnName="lName" (that's a lowercase L)
    • headerText="Last Name"
    • columnName="location"
    • headerText="Location"
    <mx:DataGridColumn columnName="lName" headerText="Last Name" />
    <mx:DataGridColumn columnName="location" headerText="Location" />
    
  6. Copy the entire DataGrid code block to the clipboard and paste it inside the Panel with id="engPanel".
  7. In the new DataGrid container, remove the dataProvider block, the id property, and the dragEnabled property, and add the dragEnter and dragDrop properties with values as follows:

    • dragEnter="dataController.doDragEnter(event)"
    • dragDrop="dataController.doDragDrop(event)"
    <mx:DataGrid dragEnter="dataController.doDragEnter(event)" 
      dragDrop="dataController.doDragDrop(event)">
      <mx:columns>
        <mx:Array>
          <mx:DataGridColumn columnName="fName" headerText="First Name" />
          <mx:DataGridColumn columnName="lName" headerText="Last Name" />
          <mx:DataGridColumn columnName="location" headerText="Location" />
        </mx:Array>
      </mx:columns>
    </mx:DataGrid>
    
  8. Copy the entire DataGrid code block from the Engineering Group Panel to the clipboard and paste it inside the Panel with id="supPanel". No changes to the code are required.

    Note: You should now have three DataGrid blocks, one in each of the Panels. The second and third have identical code. Look at the full code listing for data.mxml if you are not sure you have the correct code.